package com.vue.vue.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vue.vue.consts.MallConst;
import com.vue.vue.enums.ResponseEnum;
import com.vue.vue.from.UserLoginForm;
import com.vue.vue.from.UserRigesterForm;
import com.vue.vue.pojo.User;
import com.vue.vue.service.IUserService;
import com.vue.vue.vo.ResponseVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/user/register")
    public ResponseVo register(@Valid @RequestBody UserRigesterForm userRigesterForm,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.info("注册的参数有误{}",
                    Objects.requireNonNull(bindingResult.getFieldError()).getField()+
                            bindingResult.getFieldError().getDefaultMessage());
            return ResponseVo.error(ResponseEnum.PARAMS_ERROR,bindingResult);
        }

        User user = new User();
        BeanUtils.copyProperties(userRigesterForm,user);
        return userService.register(user);

    }

    @ApiOperation(value = "获取登录")
    @PostMapping("/admin/login")
    public ResponseVo login(@Valid @RequestBody UserLoginForm userLoginForm, BindingResult bindingResult,
                            HttpSession session){
        if(bindingResult.hasErrors()){

            return ResponseVo.error(ResponseEnum.PARAMS_ERROR,bindingResult);
        }
        ResponseVo<User> userResponseVo = userService.login(userLoginForm.getUsername(), userLoginForm.getPassword());
        //设置session
        session.setAttribute(MallConst.CURRENT_USER,userResponseVo.getData());
        log.info("/login{}"+session.getId());
        return userResponseVo;
    }

    @ApiOperation(value = "当前用户")
    @GetMapping("/user")
    public ResponseVo<User> userInfo(HttpSession session){
        log.info("/user{}"+session.getId());
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        if(user == null){
            return ResponseVo.error(ResponseEnum.NEED_LOGIN);
        }

        return ResponseVo.success(user);

    }

    @ApiOperation(value = "获取用户详情PageHelper")
    @GetMapping("/users")
    public PageInfo userInfos(@RequestParam(value = "pn", defaultValue = "1") Integer pn, HttpSession session){

        //分页插件
        PageHelper.startPage(pn,3);
        log.info("/user{}"+session.getId());
        //查询测试数据
        List<User> users = userService.selectAll();

        // 连续显示的页数
        PageInfo pageInfo = new PageInfo(users,5);

        return pageInfo;
    }

    @ApiOperation(value = "获取用户详情")
    @GetMapping("/userAll")
    public List<User> users(@RequestParam(value = "pn", defaultValue = "1") Integer pn, HttpSession session){

        log.info("/user{}"+session.getId());
        //查询测试数据
        List<User> users = userService.selectAll();
        return users;
    }

    //TODO 判断登录状态
    @ApiOperation("用户退出")
    @PostMapping("/user/logout")
    /**
     * {@link org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory}
     */
    public ResponseVo<User> logout(HttpSession session){

        session.removeAttribute(MallConst.CURRENT_USER);
        return ResponseVo.success();
    }
}
